const exec = require('child_process').exec;
const path=require('path');
const chalk = require('chalk');

/**
 * 主入口
 */
async function main(cmd,dir){
    let basename = path.basename(dir)
    let extname = path.extname(dir)
    let filePath = path.join(process.cwd(), dir)
    if(!['.js'].includes(extname)){
        console.log(chalk.red(`暂时只支持单个js文件`))
        process.exit();
    }
    if(cmd.output=="false"){
        cmd.output = process.cwd()+'/'+basename.split(extname)[0]+'.min'+extname;
    }
    console.log(chalk.green(`${cmd.compress?'已':'未'}启用压缩`))
    console.log(chalk.green(`${cmd.mangle?'已':'未'}启用混淆`))
    console.log(chalk.green(`输出文件为：${cmd.output}`))
    // console.log(process.cwd())
    // console.log(__dirname)
    exec(`npm run uglify -- ${filePath} ${cmd.compress?'-c':''} ${cmd.mangle?'-m':''} -o ${cmd.output}`, {
        cwd: __dirname,
    }, (error,stdout,stderr) => {
        if (stdout) {
            console.log(chalk.green(stdout)) 
        }else if (stderr) {
            console.log(chalk.yellow(stderr)) 
        }else if (error) {
            console.log(chalk.red(error))
        }
    });
}

module.exports = main;